﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using GraphGeneric;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Input;

namespace GraphGenericTestApp
{
	public class GraphCanvasOld : Canvas
	{
		public GraphCanvasOld()
			: base()
		{
			RenderTransform = new ScaleTransform();

			m_graph = new GraphOld();
            Random rand = new Random();
            for (int i = 0; i < 10; i++)
            {
                m_graph.AddVertex(rand.NextDouble().ToString());
            }
            //m_graph.AddVertex("Mitch");
            //m_graph.AddVertex("Thea");
            //m_graph.AddVertex("Gunner");
            //m_graph.AddVertex("Super Duper Pooper Scooper");

			//m_graph.AddEdge("Mitch", "Thea", "wife", true);
			//m_graph.AddEdge("Mitch", "Gunner", "cat", true);
			//m_graph.AddEdge("Mitch", "Super Duper\nPooper Scooper", "cat", true);

			//m_graph.AddEdge("Thea", "Mitch", "husband", true);
			//m_graph.AddEdge("Thea", "Gunner", "cat", true);
			//m_graph.AddEdge("Thea", "Super Duper\nPooper Scooper", "cat", true);

			//m_graph.AddEdge("Gunner", "Mitch", "owner", true);
			//m_graph.AddEdge("Gunner", "Thea", "owner", true);
			//m_graph.AddEdge("Gunner", "Super Duper\nPooper Scooper", "sister", true);

			//m_graph.AddEdge("Super Duper\nPooper Scooper", "Mitch", "owner", true);
			//m_graph.AddEdge("Super Duper\nPooper Scooper", "Thea", "owner", true);
			//m_graph.AddEdge("Super Duper\nPooper Scooper", "Gunner", "brother", true);

			InitializeNodes();
		}

		#region Properties

		public double Scale
		{
			get { return m_scale; }
			set
			{
				// TODO: Make animating optional with a flag field
				DoubleAnimation anim = new DoubleAnimation();
				anim.Duration = new Duration(TimeSpan.FromSeconds(0.25));
				anim.AccelerationRatio = 0.1;
				anim.DecelerationRatio = 0.9;
				anim.From = m_scale;
				anim.To = value;
				//ScaleTransform scale = new ScaleTransform();
				//scale.BeginAnimation(ScaleTransform.ScaleXProperty, anim);
				//scale.BeginAnimation(ScaleTransform.ScaleYProperty, anim);
				//RenderTransform = scale;

				RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, anim);
				RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, anim);

				m_scale = value;
			}
		}

		#endregion

		#region Public Methods

		public void SetNodeLocatations()
		{
			Random rand = new Random();
			PointAnimation anim = new PointAnimation();
			anim.Duration = new Duration(TimeSpan.FromSeconds(.75));

			foreach (GraphNodeCanvasOld node in Children.OfType<GraphNodeCanvasOld>())
			{
				anim.To = new Point(rand.NextDouble() * ActualWidth, rand.NextDouble() * ActualHeight);
				node.BeginAnimation(GraphNodeCanvasOld.LocationProperty, anim);
			}
		}

		#endregion

		#region Private Methods

		private void InitializeNodes()
		{
			//double size = .75;
			//double delta = (1 - size) / m_graph.AllVertices.Count();
			foreach (var vertex in m_graph.AllVertices)
			{
				GraphNodeCanvasOld node = new GraphNodeCanvasOld(vertex, this);
				//node.Opacity = size;
				//node.RenderTransform = new ScaleTransform(size, size);
				Children.Add(node);
				m_selectedNode = node;
				//size += delta;
			}
		}
		public void DrawEdges()
		{
			List<Line> lines = new List<Line>();
			foreach (GraphNodeCanvasOld node in Children.OfType<GraphNodeCanvasOld>())
			{
				Line line = new Line();
				line.Stroke = Brushes.HotPink;
				line.StrokeThickness = 0.5;
				line.X1 = m_selectedNode.Location.X;
				line.X2 = node.Location.X;
				line.Y1 = m_selectedNode.Location.Y;
				line.Y2 = node.Location.Y;
				lines.Add(line);
                Panel.SetZIndex(line, -1);
			}
			foreach (var l in lines)
				Children.Add(l);
		}
		private void CalibrateNodeLocations()
		{

		}

		#endregion

		#region Fields

		private IGraphOld<string, string> m_graph;
		private double m_scale = 1;
		GraphNodeCanvasOld m_selectedNode;

		#endregion
	}
}
